home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2194 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.6 KB

  1. Path: ub239.dialup.uwa.edu.au!localhost!prye
  2. From: prye@cyllene.uwa.edu.au (Peter Rye)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Problem with c code, please help!
  5. Date: 19 Jan 1996 18:02:50 GMT
  6. Organization: The University of Western Australia
  7. Message-ID: <PRYE.96Jan20020251@ub239.dialup.uwa.edu.au>
  8. References: <surgsw-1901960148530001@128.206.206.86>
  9.     <mikedorman-1901960725380001@205.148.200.150>
  10. NNTP-Posting-Host: ub239.dialup.uwa.edu.au
  11. In-reply-to: mikedorman@cedarnet.com's message of Fri, 19 Jan 1996 07:25:37 -0600
  12.  
  13.  
  14. Hi Mike,
  15.  
  16.  
  17. >   But, if you did it with a string pointer, it's this much easier:
  18. >
  19. >   main()
  20. >   { 
  21. >      char *word;
  22. >
  23. >      puts("Enter a word: ");
  24. >      while(scanf("%s", word) != 0)
  25. >
  26. >      puts("You entered: ");
  27. >      puts(word);
  28. >   }
  29.  
  30. I'm not sure that this is good advice.
  31.  
  32. It gives a segmentation fault here, and/or goes into an infinite loop.
  33. Although it is ``easier'' to try to manage without allocating an array
  34. for your string, it will cause some problems when you try to stuff an
  35. arbitrary length string into a char * .
  36.  
  37. scanf() will only return 0, and therefore exit the while loop when no
  38. assignments are made on input data.
  39.  
  40. This ``advice'' comes to you from someone who made himself look a little
  41. foolish last week with some rubbish posted to the group, so take it with
  42. a large grain of salt :-).
  43.  
  44. Regards,
  45. Peter Rye
  46. --
  47. | Peter Rye                                                           |
  48. | Respiratory Research Fellow                                         |
  49. | Princess Margaret Hospital for Children                             |
  50. | Perth, Western Australia                                            |
  51.